package com.higgins.server;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

import com.higgins.client.dataModel.StrategyDTO;

@Entity
@Table(name = "strategys")
public class Strategy {

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
	private Long id;
    
    private String name; 
    
    @ManyToOne
    @JoinColumn(name="letterId")
    private CoveringLetter coveringLetter;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="timestamp")
    private Date timestamp;
    
    @Column(name="status")
    private Integer status;

    public Strategy() {
        // This is used by JPA
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }
    
    public CoveringLetter getCoveringLetter()	{
    	return coveringLetter;
    }
    
    public void setCoveringLetter(CoveringLetter coveringLetter)	{
    	this.coveringLetter = coveringLetter;
    }
    public Integer getStatus()	{
    	return status;
    }
    public void setStatus(Integer status)	{
    	this.status = status;
    }
    public StrategyDTO toDTO()	{
    	StrategyDTO dto = new StrategyDTO();
    	dto.setId(id);
    	dto.setName(name);
    	dto.setTimestamp(timestamp);
    	dto.setStatus(status);
    	if (coveringLetter!=null)	{
    		dto.setCoveringLetterDTO(coveringLetter.toDTO());
    	}
    	return dto;
    }
    public void update(StrategyDTO dto)	{
    	this.name = dto.getName();
    	this.timestamp = dto.getTimestamp();
    	this.status = dto.getStatus();
    	if (dto.getCoveringLetterDTO()!=null)	{
    		// Need to update the covering letter here!!
    	} else {
    		coveringLetter = null;
    	}
    }

}